package com.goldsprite.microhorigame.ecs;

import com.goldsprite.gameframeworks.ecs.component.Component;
import com.goldsprite.gameframeworks.input.GameInputSystem;
import com.goldsprite.gameframeworks.input.InputActionState;
import com.goldsprite.gameframeworks.log.Debug;
import com.goldsprite.gameframeworks.utils.math.Vector2;

import java.util.function.Consumer;

public class EntityInputComponent extends Component {
	private boolean enableUserInput;
	private InputMode inputMode = InputMode.PlayerInput;

	private float lastFaceX;
	private float key_dirX;
	private boolean key_attack, key_speedBoost, key_jump, key_hurt, key_death;
	private boolean moveKeyProtect;

	int doubleKeyTicker;
	float doubleKeyDir;
	float doubleKeyIntervalSeconds = 0.2f;
	long startMillis, diffMillis;
	private transient final Consumer<Vector2> onMove = vector -> {
		if(!enableUserInput) return;
		float keyDirX = vector.x;
		float norKeyDirX = Math.signum(keyDirX);
		int faceX = (int)Math.signum(keyDirX);
		if(faceX != lastFaceX) {
			setMoveKeyProtect(false);
		}
		lastFaceX = faceX;
		setKeyDirX(keyDirX);

//		long curMillis = System.currentTimeMillis();
//		float tickTime = (float)(curMillis - startMillis)/1000f;
//		if(tickTime >= doubleKeyIntervalSeconds) {
//			//Debug.log("计数器时间已过");
//			doubleKeyTicker = 0;
//		}
//		if(doubleKeyTicker == 0 && norKeyDirX != 0) {
//			doubleKeyDir = norKeyDirX;
//			startMillis = System.currentTimeMillis();
//			doubleKeyTicker = 1;
//		}else if(doubleKeyTicker == 1 && norKeyDirX == 0) {
//			doubleKeyTicker = 2;
//		}else if(doubleKeyTicker == 2 && norKeyDirX == doubleKeyDir) {
//			if(tickTime < doubleKeyIntervalSeconds){
//				if(!key_speedBoost){
//					setKeySpeedBoost(true);
//					Debug.log("双击移动疾跑: 方向: %s", norKeyDirX);
//				}
//			}
//		}

		boolean moveUp = !isMoveActionStared();
		if(key_speedBoost && moveUp){
			setKeySpeedBoost(false);
		}
	};

	private transient final Consumer<Boolean> onBoostMove = down -> {
//		if(down) setKeySpeedBoost(true);
//		else if(!isMoveActionStared()) setKeySpeedBoost(false);
		setKeySpeedBoost(down);
	};
	private transient final Consumer<Boolean> onAttack = down -> {
		if(!enableUserInput) return;
		setKeyAttack(down);
	};
	private transient final Consumer<Boolean> onJump = down -> {
		if(!enableUserInput) return;
		setKeyJump(down);
	};

	@Override
	public void awake() {
		super.awake();

		if(inputMode.equals(InputMode.PlayerInput)){
			GameInputSystem inputSystem = GameInputSystem.getInstance();

			GameInputSystem.registerActionListener(inputSystem.getInputAction("Move"), InputActionState.All, onMove);
			GameInputSystem.registerActionListener(inputSystem.getInputAction("SpeedBoost"), InputActionState.All, onBoostMove);
			GameInputSystem.registerActionListener(inputSystem.getInputAction("Attack"), InputActionState.All, onAttack);
			GameInputSystem.registerActionListener(inputSystem.getInputAction("Jump"), InputActionState.All, onJump);
//			inputSystem.registerActionListener(inputSystem.getInputAction("SpeedBoost"), InputActionState.All, onSpeedBoost);
//			inputSystem.registerActionListener(inputSystem.getInputAction("Respawn"), InputActionState.All, onRespawn);
//			inputSystem.registerActionListener(inputSystem.getInputAction("Sit"), InputActionState.All, onSit);
		}else if(inputMode.equals(InputMode.AIInput)){

		}
	}

	public void setEnableUserInput(boolean enableUserInput) {
		this.enableUserInput = enableUserInput;
	}

	public float getKeyDirX() {
		return key_dirX;
	}
	public void setKeyDirX(float key_dirX) {
		this.key_dirX = key_dirX;
	}
	public boolean isMoving() {
		return key_dirX != 0;
	}

	public boolean getKeySpeedBoost() {
		return key_speedBoost;
	}
	public void setKeySpeedBoost(boolean key_speedBoost) {
		this.key_speedBoost = key_speedBoost;
	}

	public boolean getKeyAttack() {
		return key_attack;
	}
	public void setKeyAttack(boolean down) {
		key_attack = down;
	}

	public boolean getKeyJump() {
		return key_jump;
	}
	public void setKeyJump(boolean down) {
		key_jump = down;
	}

	public boolean getKeyHurt() {
		return key_hurt;
	}
	public void setKeyHurt(boolean down) {
		key_hurt = down;
	}

	public boolean getMoveKeyProtect() {
		return moveKeyProtect;
	}
	public void setMoveKeyProtect(boolean moveKeyProtect) {
		this.moveKeyProtect = moveKeyProtect;
	}

	public InputMode getInputMode() {
		return inputMode;
	}
	public void setInputMode(InputMode inputMode) {
		this.inputMode = inputMode;
	}

	public boolean getKeyDeath() {
		return key_death;
	}
	public void setKeyDeath(boolean down) {
		this.key_death = down;
	}

	public boolean isKeyDeath() {
		return key_death;
	}

	private boolean isMoveActionStared() {
		return GameInputSystem.getInstance().getInputAction("Move").isStarted();
	}

	public enum InputMode {
		PlayerInput, AIInput
	}
}
